package cn.com.css.taxInspection.workflow.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.common.exception.BusinessException;

import cn.com.css.taxInspection.workflow.entity.WorkflowThresholdDetailEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowThresholdEntity;
import cn.com.css.taxInspection.workflow.mapper.WorkflowThresholdDetailMapper;
import cn.com.css.taxInspection.workflow.mapper.WorkflowThresholdMapper;
import cn.com.css.taxInspection.workflow.service.WorkflowThresholdDetailService;



@Service("workflowThresholdDetailService")
public class WorkflowThresholdDetailServiceImpl extends ServiceImpl<WorkflowThresholdDetailMapper, WorkflowThresholdDetailEntity> implements WorkflowThresholdDetailService {
	@Resource
    private WorkflowThresholdDetailMapper workflowThresholdDetailMapper;
    @Resource
    private WorkflowThresholdMapper workflowThresholdMapper;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public IPage<WorkflowThresholdDetailEntity> listByPage(Page page, String thresholdId) {

		WorkflowThresholdEntity sysDictEntity = workflowThresholdMapper.selectById(thresholdId);
        if (sysDictEntity == null) {
            throw new BusinessException("获取字典数据失败!");
        }

        LambdaQueryWrapper<WorkflowThresholdDetailEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WorkflowThresholdDetailEntity::getThresholdId, thresholdId);
        wrapper.orderByAsc(WorkflowThresholdDetailEntity::getSort);
        IPage<WorkflowThresholdDetailEntity> result = workflowThresholdDetailMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(result.getRecords())) {
            result.getRecords().parallelStream().forEach(entity -> entity.setThresholdName(sysDictEntity.getName()));
        }
        
        return result;
    }

}